//
//  YAYIApilySuccessController.m
//  YAYIDoctor
//
//  Created by hxp on 16/5/6.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIApilySuccessController.h"
#import "YAYIApilyListController.h"
@interface YAYIApilySuccessController ()
@property (nonatomic,strong)UIView *bottomView;
@property (nonatomic,strong)UILabel *status;
@property (nonatomic,strong)UIButton *lastBtn;
@end

@implementation YAYIApilySuccessController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    
    [self setupNav];
    [self createView];
}


- (void)setupNav
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"back_button"] forState:UIControlStateNormal];
    
    button.frame = (CGRect){{0, 0}, CGSizeMake(70, 30)};
    
    // 让按钮内部的所有内容左对齐
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    //        [button sizeToFit];
    // 让按钮的内容往左边偏移10
    //        button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    // 修改导航栏左边的item
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}





-(void)createView{
    
    UIImageView *icon = [UIImageView new];
    icon.image = [UIImage imageNamed:@"subSuccess"];
    [self.view addSubview:icon];
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(20));
        make.top.equalTo(@(25 + 64));
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    if (self.status == nil) {
        self.status = [UILabel new];
        self.status.textColor = [UIColor blackColor];
        self.status.font = [UIFont systemFontOfSize:font18];
        self.status.text = @"创建收费成功";
        [self.view addSubview:self.status];
    }
    [self.status mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(icon.mas_centerY);
        make.left.mas_equalTo(icon.mas_right).with.offset(15);
        make.size.mas_equalTo(CGSizeMake(200, 30));
    }];
    if (self.bottomView == nil) {
        self.bottomView = [UIView new];
        self.bottomView.userInteractionEnabled = YES;
        [self.view addSubview:self.bottomView];
    }
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.top.mas_equalTo(self.view.mas_bottom).with.offset(-70);
        make.height.equalTo(@(70));
        
    }];
    NSArray *title = @[@"返回主页",@"继续创建"];
    CGFloat w = (SCREEN_width - 80)/2.0;
    for (int i=0; i< title.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:title[i] forState:UIControlStateNormal];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 5;
        [button setTitleColor:YAYIColor(83, 165, 243) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:font16];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage imageNamed:@"register_button_normal"] forState:UIControlStateSelected];
        button.layer.borderWidth = 0.5;
        if (i == 1) {
            button.selected = YES;
        }
        button.tag = i;
        button.adjustsImageWhenHighlighted = NO;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.borderColor = YAYIColor(83, 165, 243).CGColor;
        [self.bottomView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(30 + (w + 20)*i));
            make.top.mas_equalTo(self.bottomView.mas_top);
            make.size.mas_equalTo(CGSizeMake(w , 36));
        }];
    }
}


#pragma mark - 点击事件
- (void)back
{
    
    NSArray *childVcs = self.navigationController.childViewControllers;
    YAYIApilyListController *aplilyListVc = childVcs[childVcs.count - 3];
    [aplilyListVc refreshPage];
    [self.navigationController popToViewController:aplilyListVc animated:YES];
}


-(void)buttonAction:(UIButton *)sender
{
    self.lastBtn.selected = NO;
    [self.lastBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    sender.selected = YES;
    if (sender.selected) {
        if (sender.tag == 1) {
            
            NSArray *childVcs = self.navigationController.childViewControllers;
            YAYIApilyListController *aplilyListVc = childVcs[childVcs.count - 3];
            [aplilyListVc refreshPage];
            [self.navigationController popToViewController:aplilyListVc animated:YES];
        }else{
            
            [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            [sender setBackgroundImage:[UIImage imageNamed:@"register_button_normal"] forState:UIControlStateSelected];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        
    }
    self.lastBtn = sender;
    
}
@end
